<!--<script type="text/javascript" src="public/ajax/admin.js"></script>-->
<script>
    function submitNews(){
        var date = document.getElementById("date").value;
        var news = document.getElementById("news").value;
        if(date == '' || news == ''){
            alert("Date or news can't be empty!");
        } else {
            var r = confirm("Are you confirm to submit news?");
            if(r == true){
                var success = "admin/addNews?date=" + date + "&news=" + news;
                $.get(success)
                .success(function(data) {
                    if(data){
                        alert("News has been submitted!");
                        var page = baseurl + 'admin/showNews';
                        $.get(page)
                        .success(function(data) {
                            $('div#content').html(data);
                        });
                    }
                });
            }
        }

    }
    
    function deleteNews(id){
        var r = confirm("Are you confirm to delete this news?");
        if(r == true){
            var success = baseurl+"admin/deleteNews?id=" + id;
            //            alert(success)
            $.get(success)
            .success(function(data) {
                alert(data)
                if(data == "success"){
                    alert("Successfully deleted!");
                    var page = baseurl + 'admin/showNews';
                    $.get(page)
                    .success(function(data) {
                        $('div#content').html(data);
                    });
                } else {
                    alert("No change made!");
                }
            });
        } 
        return false;
    }
    
    function updateNews(id){
        var news = document.getElementById(id).innerHTML;
        var date = document.getElementsByName(id)[0].value;
        var r = confirm("Are you confirm to update news?");
        if(r == true){
            var success = baseurl+"admin/updateNews?date=" + date + "&news=" + news + "&id=" + id;
            //            alert(success)
            $.get(success)
            .success(function(data) {
                if(data == "success"){
                    alert("Successfully updated!");
                    var page = baseurl + 'admin/showNews';
                    $.get(page)
                    .success(function(data) {
                        $('div#content').html(data);
                    });
                } else {
                    alert("No change made!");
                }
            });
        }
        return false;
    }
    //    jQuery(document).ready(function(){
    //
    //        jQuery('.ajaxform').submit( function() {
    //            var r = confirm("Are you confirm to submit news?");
    //            if(r == true){
    //                alert(str);
    //                $.ajax({
    //                    url     : $(this).attr('action'),
    //                    type    : $(this).attr('method'),
    //                    data    : $(this).serialize(),
    //                    success : function( data ) {
    ////                        alert(data);
    //                        alert('Form is successfully submitted');       
    //                    },
    //                    error   : function(){
    //                        alert('Something wrong');
    //                    }
    //                });
    //            }
    //            return false;
    //        });
    //
    //    });
</script>

<h4>Recent News</h4>
<hr>
<div class="row">
    <form method='none' disabled>
        <fieldset>
            <legend>Add News</legend>
            <ul class="inline">
                <li class="field">
                    <label class="inline">Date&nbsp;</label>
                    <input class="narrow text input" type="date" placeholder="Text input" name="date" id="date"/>
                </li>
                <li class="field">
                    <label class="inline" style="vertical-align: top">News</label>
                    <textarea class="wide input textarea" type="textarea" placeholder="Text input" name="news" id="news"></textarea>
                </li>
                <li>
                    <div class="pretty medium default btn"  style="margin-left: 30px"><input type="button" value="Submit" onclick="submitNews()"></div>
                </li>
            </ul>
        </fieldset>
    </form>
</div>
<?php
// put your code here
if (isset($data) && count($data)) {
    ?>
    <table class="dataTable" id="news">
        <thead>
        <th>Date</th>
        <th>News</th>
        <th></th>
        <th></th>
    </thead>
    <tbody>
        <?php
        foreach ($data as $row) {
            $id = $row['id'];
            $date = $row['date'];
//                    $date.
            echo "<tr>";
            echo "<td><input type='date' name='$id' value='$date' style='border: 0px'>";
//                    echo $row['date'];
            echo "</td>";
            echo "<td contenteditable='true' id='$id'>";
            echo $row['news'];
            echo "</td>";
            echo "<td>";
            echo "<a href='#' onclick='updateNews($id)'>Update</a>";
            echo "</td>";
            echo "<td>";
            echo "<a href='#' onclick='deleteNews($id)'>Delete</a>";
            echo "</td>";
            echo "</tr>";
        }
        ?>
    </tbody>
    </table>
    <?php
}
?>

